#include "core_debug.h"
#include "ch32v_def.h "
#include "variant.h"
#include "wiring_time.h"
#ifdef __cplusplus
extern "C"
{
#endif

#ifdef SERIAL_UART_INSTANCE
// __attribute__((used)) 
int core_write(int fd, char *buf, int size)
{
    for (int i = 0; i < size; i++)
    {
#if (SERIAL_UART_INSTANCE == 1)
        while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
            ;
        USART_SendData(USART1, *buf++);
#elif (SERIAL_UART_INSTANCE == 2)
        while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET)
            ;
        USART_SendData(USART2, *buf++);
#elif (SERIAL_UART_INSTANCE == 3)
        while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET)
            ;
        USART_SendData(USART3, *buf++);
#endif
    }
    return size;
}

__attribute__((used)) 
void *_sbrk(ptrdiff_t incr)
{
    extern char _end[];
    extern char _heap_end[];
    static char *curbrk = _end;

    if ((curbrk + incr < _end) || (curbrk + incr > _heap_end))
    return NULL - 1;

    curbrk += incr;
    return curbrk - incr;
}

#endif

#ifdef __cplusplus
}
#endif
